<?php

// Include config file
include('./common.php');

// Connect to database
$link = dbConnect();

$user = auth($_POST['username'],$_POST['password']);
if($user == ""){
	echo "output=badLogin";
	mysql_close($link);
	return;
}

//check if they are a global admin
if(checkAdminPermissions($user['id']) == 0) {
	//tell flash they are not an admin and stop processing
	echo "output=permissionError";
	mysql_close($link);
	return;
}else{
	//let's see what flash needs
	$output = "";
	
	if(isset($_POST['location'])){
		$output .= "output=".$_POST['location'];
		
		switch($_POST['location']){
			case "forums":
				$output .= getGroups();
				$output .= getCategories();
				break;
			
			case "groups":
				$output .= getGroups();
				break;
			
			case "configuration":
				$output .= getConfig();
				break;
				
			case "users":
				$output .= getUserData();
				break;
		}
	}
	
	if(isset($_POST['action'])){
		//see what action we need to do
		switch($_POST['action']){
			case "addCategory":
				include_once('./admin/categories.php');
				$output .= addCategory($_POST['categoryName'], $_POST['categoryPosition']);	
				break;
			
			case "deleteCategory":
				include_once('./admin/categories.php');
				$output .= deleteCategory($_POST['categoryID'], $_POST['deleteForums']);
				$output .= getCategories();
				$output .= getGroups();	
				break;
			
			case "addForum":
				include_once('./admin/forums.php');
				$output .= addForum($_POST['forumName'], $_POST['forumDescription'], $_POST['forumReadMode'], $_POST['forumPosition'],
									$_POST['categoryID'], $_POST['groupID'], $_POST['moderatorGroupID']);	
				break;
				
			case "deleteForum":
				include_once('./admin/forums.php');
				$output .= deleteForum($_POST['categoryID'], $_POST['forumID'], $_POST['deleteThreads']);
				$output .= getCategories();
				$output .= getGroups();		
				break;
				
			case "addGroup":
				include_once('./admin/groups.php');
				$output .= addGroup($_POST['groupName'], $_POST['groupDescription']);
				$output .= getGroups();
				break;
				
			case "saveGroup":
				include_once('./admin/groups.php');
				$output .= saveGroup($_POST['groupID'], $_POST['groupName'], $_POST['groupDescription']);
				$output .= getGroups();
				break;
			
			case "deleteGroup":
				include_once('./admin/groups.php');
				$output .= deleteGroup($_POST['groupID']);
				$output .= getGroups();
				break;
				
			case "getGroupUsers":
				include_once('./admin/groups.php');
				$output .= getUsers($_POST['groupID']);
				break;
				
			case "saveConfig":
				include_once('./admin/config.php');
				$output .= saveConfig($_POST);
				$output .= getConfig();
				break;
		}
	}
	
	$output .= "&admin=1";
}


echo $output;

mysql_close($link);


function getForums($categoryID){
	//setup a var to send the data back to flash
	$output = "";
	
	//get all the forums under a category	
	$result = mysql_query("SELECT forumID, title, displayOrder FROM ".TABLE_PREFIX."_forums WHERE categoryID = ".$categoryID." ORDER BY displayOrder ASC");
	
	$totalForums = mysql_num_rows($result);
	$output .= "&category".$categoryID."TotalForums=".$totalForums;
	
	//loop through the forums and spit them out
	for($i = 0; $i < $totalForums; $i++){
		$data = mysql_fetch_object($result);
		
		$output .= "&category".$categoryID."Forum".$i."ID=".$data->forumID;
		$output .= "&category".$categoryID."Forum".$i."Name=".urlencode($data->title);
		$output .= "&category".$categoryID."Forum".$i."DisplayOrder=".$data->displayOrder;
	}	
	
	return $output;
}

function getCategories(){
	$output = "";
	//get a list of categories even if they don't have forums
	$result = mysql_query("SELECT * FROM ".TABLE_PREFIX."_categories ORDER BY displayOrder ASC");
	$categoryTotal = mysql_num_rows($result);
	
	for($i = 0; $i < $categoryTotal; $i++){
		$data = mysql_fetch_object($result);
		$output .= "&category".$i."ID=".$data->categoryID;
		$output .= "&category".$i."Name=".urlencode($data->name);
		$output .= "&category".$i."DisplayOrder=".$data->displayOrder;
		
		//also output any forums we may have
		$output .= getForums($data->categoryID);
	}

	$output .= "&categoryTotal=".$categoryTotal;
	
	return $output;
}


function getGroups(){
	$output = "";
	//get a list of groups
	$result = mysql_query("SELECT groupID, name, description FROM ".TABLE_PREFIX."_groups");
	$groupTotal =  mysql_num_rows($result);
	
	for($i = 0; $i < $groupTotal; $i++){
		$data = mysql_fetch_object($result);
		$output .= "&group".$i."ID=".$data->groupID;
		$output .= "&group".$i."Name=".urlencode($data->name);
		$output .= "&group".$i."Description=".urlencode($data->description);
	}
	
	$output .= "&groupTotal=".$groupTotal;
	
	return $output;
}


function getConfig(){
	$output = "";
	//send all the config vars back to Flash
	$result = mysql_query("SELECT * FROM ".TABLE_PREFIX."_config");
	$configTotal = mysql_num_rows($result);

	for($i = 0; $i < $configTotal; $i++){
		$data = mysql_fetch_object($result);
		$output .= "&config".$i."Name=".urlencode($data->configName);
		$output .= "&config".$i."Value=".urlencode($data->configValue);
	}
	
	$output .= "&configTotal=".$configTotal;
	
	return $output;
}

function getUserData(){
	$output = "";
	
	$result = mysql_query("SELECT * FROM ".TABLE_PREFIX."_users");
	$userTotal = mysql_num_rows($result);
	
	$output .= "&userTotal=".$userTotal;
	
	return $output;	
}
?>